<template>
	<view>
		<view class="submit">
			<view class="submit-chat">
				<!-- <view class="bt-img">
					<image src="/static/tabBar/index1.png" @click="test"></image>
				</view> -->
				<!-- 文本框 -->
				<textarea 
					:contenteditable="true"
					cursor-spacing="300"
					:adjust-position="true"
					:disable-default-padding="true" 
					:show-confirm-bar="false" 
					confirm-type="send" 
					:auto-height="true" 
					@focus="focus"
					@blur="blur"
					@input="inputs"
					@confirm="confirm"
					v-model="msg"
					class="chat-send btn"  
				></textarea>
				<view class="bt-img" @click="emoji">
					<image src="/static/chat/emoji.png"></image>
				</view>
				<view class="bt-img" :class="{'displaynone':issend}" @click="more">
					<image src="/static/chat/plus.png"></image>
				</view>
				<view class="send-bt" :class="{'displaynone':!issend}" @touchend.prevent="handleSend">发送</view>
			</view>
			<!-- 表情 -->
			<view class="emoji" :class="{'displaynone':!isemoji}">
				<view class="emoji-send">
					<view class="emoji-send-det" @click="emojiBack">
						<image src="/static/chat/back.png"></image>
					</view>
					<view class="send-bt emoji-send-bt" @click="emojiSend">发送</view>
				</view>
				<emoji @emotion="emotion" :height="260"></emoji>
			</view>
			<!-- 更多 -->
			<view class="more" :class="{'displaynone':!ismore}">
				<view class="more-list">
					<image src="/static/chat/photo.png" @click="sendImg('album')"></image>
					<view class="more-list-title">图片</view>
				</view>
				<view class="more-list" @click="sendImg('camera')">
					<image src="/static/chat/camera.png"></image>
					<view class="more-list-title">拍照</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	// 引入组件
	import emoji from './emoji.vue'

	export default {
		data() {
			return {
				issend: false,  //发送按钮
				isemoji: false,  //表情包页面
				ismore: false,  //更多功能页面
				pageY: 0,
				msg: '',
				timer: '', //计时器
				vlength: 0
			};
		},
		components: {
			emoji,
		},
		watch: {
			msg(newVal, oldVal) {
				if (newVal.length > 0) {
					this.issend = true
				} else {
					this.issend = false
				}
			}
		},
		methods: {
			//获取高度方法
			getElementHeight() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.submit').boundingClientRect(data => {
					this.$emit('heights', data.height);
				}).exec();
			},
			//切换音频
			
			// 聚焦
			focus() {
				console.log('获取焦点');
				this.isemoji = false;
				this.ismore = false;
				setTimeout(() => {
					this.getElementHeight()
				}, 10)
			},
			// 失去焦点
			blur(e) {
				console.log('失去焦点');
				// var chatm = e.detail.value;
				// this.msg = chatm
			},
			// 键盘输入(ios有bug)
			inputs(e) {
				console.log('键盘输入');
				
				// var chatm = e.detail.value;
				// this.msg = chatm
				// var returnFlag = chatm.endsWith('\n');
				// if (returnFlag && chatm.length > 1) {
				// 	this.send(this.msg, 0)
				// }
			},
			// 点击完成
			confirm(e) {
				console.log('点击完成');
				let chatm = e.detail.value;
				
				if (chatm.length > 1) {
					this.send(chatm, 0)
				}
			},
			// 点击发送
			handleSend() {
				this.send(this.msg, 0)
			},
			
			// 表情窗口
			emoji() {
				this.isemoji = !this.isemoji;
				//关闭其他功能
				this.ismore = false;
				//切换高度
				setTimeout(() => {
					this.getElementHeight();
				}, 10)
			},
			//表情输入
			emotion(e) {
				this.msg = this.msg + e
				console.log(this.msg);
			},
			// 表情发送
			emojiSend() {
				if (this.msg.length > 0) {
					this.send(this.msg, 0)
				}
			},
			// 表格退格
			emojiBack() {
				if (this.msg.length > 0) {
					this.msg = this.msg.slice(0, -2);
				}
			},
			
			//更多功能
			more() {
				this.ismore = !this.ismore;
				// 关闭其他界面
				this.isemoji = false
				// this.toc = require("/static/tabBar/my1.png");
				setTimeout(() => {
					this.getElementHeight();
				}, 10)
			},
			//图片发送
			sendImg(e) {
				let count = 9;
				if (e == 'album') {
					count = 9;
				} else {
					count = 1;
				}
				uni.chooseImage({
					count: count, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: [e], //从相册选择
					// success: function (res) { //用function的方式会找不到send方法
					success: (res) => {
						const filePaths = res.tempFilePaths;
						for (let i = 0; i < filePaths.length; i++) {
							uni.uploadFile({
								url: this.$request.baseUrl + '/common/upload',
								filePath: filePaths[i],
								name: 'file',
								header: {
									'content-type': 'multipart/form-data' 
								},
								success:(res) => {
									res = JSON.parse(res.data)
									if (res.code == 200) {
										this.send(res.fileName, 1)
									}
								}
							});
						}
					}
				});
			},
			
			//发送
			send(msg, type) {
				let date = {
					message: msg,
					type: type
				}
				this.$emit('inputs', date);
				this.msg = '';
			},
			hide() {
				this.isemoji = false
				this.ismore = false
			}
		}
	};
</script>

<style lang="scss" scoped>
	.submit {
		background: rgba(244, 244, 244, 0.96);
		border-top: 1px solid rgba(39, 40, 50, 0.1);
		width: 100%;
		position: fixed;
		bottom: 0;
		z-index: 100;
		// padding-bottom: var(--status-bar-height);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.displaynone {
		display: none;
	}
	
	.send-bt {
		margin-left: 10rpx;
		width: 120rpx;
		height: 60rpx;
		line-height: 60rpx;
		color: #fff;
		background: #008676;
		font-size: 28rpx;
		text-align: center;
		border-radius: 12rpx;
	}

	.submit-chat {
		width: 100%;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 14rpx 20rpx;

		image {
			width: 63rpx;
			height: 63rpx;
			margin-left: 10rpx;
			flex: auto;
		}

		.btn {
			flex: auto;
			background-color: #fff;
			border-radius: 10rpx;
			padding: 20rpx;
			max-height: 160rpx;
			margin: 0 10rpx;
		}

		.chat-send {
			line-height: 44rpx;
			-webkit-user-select:auto !important;
		}

		.record {
			line-height: 44rpx;
			text-align: center;
			font-size: 20rpx;
			color: rgba(39, 40, 50, 0.6);
		}
	}

	.emoji {
		width: 100%;
		height: 460rpx;
		background: rgba(236, 237, 238, 1);
		box-shadow: 0px 11rpx 0px 0px rgba(0, 0, 0, 0.1);

		.emoji-send {
			width: 280rpx;
			height: 104rpx;
			padding-top: 24rpx;
			background-color: rgba(236, 237, 238, 0.8);
			position: fixed;
			// bottom: 0;
			bottom: env(safe-area-inset-bottom);
			right: 0;
			display: flex;
			
			.emoji-send-bt {
				flex: 1;
				height: 80rpx;
				line-height: 80rpx;
				margin: 0 32rpx 0 20rpx;
			}

			.emoji-send-det {
				flex: 1;
				// margin-left: 24rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 80rpx;
				background: #fff;
				font-size: 32rpx;
				text-align: center;
				line-height: 80rpx;
				border-radius: 12rpx;

				image {
					width: 42rpx;
					height: 42rpx;
				}
			}
		}
	}

	.more {
		width: 100%;
		height: 436rpx;
		background: rgba(236, 237, 238, 1);
		box-shadow: 0px 11rpx 0px 0px rgba(0, 0, 0, 0.1);
		bottom: env(safe-area-inset-bottom);
		padding: 8rpx 20rpx;
		box-sizing: border-box;

		.more-list {
			width: 25%;
			text-align: center;
			float: left;
			padding-top: 32rpx;

			image {
				width: 72rpx;
				height: 72rpx;
				padding: 24rpx;
				background: rgba(255, 255, 255, 1);
				border-radius: 24rpx;
			}

			.more-list-title {
				font-size: 24rpx;
				color: rgba(39, 40, 50, 0.5);
				line-height: 34rpx;
			}
		}
	}

	.voice-bg {
		height: 100%;
		width: 100%;
		background-color: rgba(0, 0, 0, 0.3);
		position: fixed;
		top: 0;
		bottom: 0;
		z-index: 1001;

		.voice-bg-len {
			height: 84rpx;
			width: 600rpx;
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			margin: auto;
			background-color: rgba(255, 255, 255, 0.2);
			border-radius: 42rpx;
			text-align: center;
		}

		.voice-bg-time {
			display: inline-block;
			min-width: 120rpx;
			line-height: 84rpx;
			background-color: rgba(255, 228, 49, 1);
			border-radius: 42rpx;
		}

		.voice-del {
			position: absolute;
			bottom: -480rpx;
			width: 100%;
			text-align: center;
			color: #fff;
			font-size: 28rpx;
		}
	}
</style>
